import $C from '@/common/lib/config.js'
import $S from './storage.js'

export default {
	common: {
		method: 'GET',
		header: {
			"content-type": "application/json"
		},
		data: {}
	},
	request(options = {}) {
		//console.log(options.url,options)
		options.url = $C.getBaseUrl() + options.url
		options.method = options.method || 'GET'
		options.header = options.header || {
			"content-type": "application/json"
		}
		// 暂时没token
		let token = $S.getStorage('token')
		if (token && !options.skipToken) {
			// console.log(token, 'token')

			options.header['Authorization'] = "Bearer " + token
			options.header['User-Authorization'] = "Bearer " + token
		}

		return new Promise((res, rej) => {
			uni.request({
				...options,
				success: (result) => {

					if (options.native) {
						return res(result)
					}
					if (result.success || result.statusCode == 200) {
						// 成功
						res(result.data)
					} else {
						// rej
						rej(new Error(result.data.message || 'Error'))
					}
				},
				fail: (error) => {
					return rej(error)
				}
			});
		})
	},
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	upload(url, options = {}) {
		options.url = $C.webUrl + url
		options.header = options.header || {}
		//验证权限token
		let token = $S.getStorage('token')
		if (token) {
			options.header.token = token
			if (!options.header.token) {
				return uni.showToast({
					title: '非法token,请重新登录',
					icon: 'none'
				});
			}
		}
		console.log('上传图片地址:' + options.url)
		return new Promise((res, rej) => {
			uni.uploadFile({
				...options,
				success: (uploadFileRes) => {
					//console.log(uploadFileRes)

					if (uploadFileRes.statusCode !== 200) {
						return uni.showToast({
							title: '上传图片失败',
							icon: 'none'
						});
					}
					let data = JSON.parse(uploadFileRes.data)
					res(data)
				},
				fail: (err) => {
					rej(err)
				}
			});
		})

	},
	//上传文件 需要登录验证
	uploadWithToken(url, data, onProgress = false) {
		return new Promise((result, reject) => {
			// 上传
			let token = $S.getStorage('csrc_token')
			console.log(token)
			if (!token) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
			}
			console.log($C.getBaseUrl() + url, 'url')
			// let token = $S.getStorage('csrc_token')
			const uploadTask = uni.uploadFile({
				url: $C.getBaseUrl() + url,
				filePath: data.filePath,
				name: data.name || "fileAjax",
				header: {
					'Authorization': "Bearer " + token,
					'User-Authorization': "Bearer " + token
				},
				formData: {
					allowTypes: ['pdf', 'jpg', 'jpeg', 'png', 'doc', 'docx', 'xls', 'xlsx', 'rar',
						'zip', 'txt'
					]
				},
				success: (res) => {
					if (res.statusCode !== 200) {
						result(false)
						return uni.showToast({
							title: '上传失败',
							icon: 'none'
						});
					}
					console.log(res, 'res')
					let message = JSON.parse(res.data)
					let value = JSON.parse(message.value)
					console.log(message, 'message')
					console.log(value, 'value')
					result(value);
				},
				fail: (err) => {
					console.log(err);
					reject(err)
				}
			})

			uploadTask.onProgressUpdate((res) => {
				if (typeof onProgress === 'function') {
					onProgress(res.progress)
				}
			});

		})
	}
}